package com.xinzhi.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMqConfig {
    //    //设置队列
//    public  static final String QUEUE_NAME = "fanout_queue";
//    //设置交换机
//    public  static final String EXCHANGE_NAME = "fanout_exchange";
//设置队列
    //声明交换机和队列
    public static final String EXCHANGE_NAME = "topic_exchange";
    public static final String QUEUE_NAME = "topic_queue";


    public static final String EXCHANGE_DEAD = "dead_exchange";
    public static final String QUEUE_DEAD = "dead_queue";

    @Bean("queue")
    public Queue queue(){
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", EXCHANGE_DEAD);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "dead.#");
        return QueueBuilder.durable(QUEUE_NAME).withArguments(args).build();
    }

    @Bean("exchange")
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    @Bean
    public Binding binding(@Qualifier("queue") Queue queue, @Qualifier("exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("xinzhi.#").noargs();
    }

    //声明死信交换机
    @Bean("deadLetterExchange")
    public Exchange deadLetterExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_DEAD).durable(true).build();
    }

    // 声明死信队列
    @Bean("deadLetterQueue")
    public Queue deadLetterQueueA(){
        return new Queue(QUEUE_DEAD);
    }

    // 声明死信队列绑定关系
    @Bean
    public Binding deadLetterBinding(@Qualifier("deadLetterQueue") Queue queue,
                                     @Qualifier("deadLetterExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("dead.#").noargs();
    }


}

//    @Bean
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate template = new RabbitTemplate(connectionFactory);
//        template.setMandatory(true);
//        template.setConfirmCallback(confirmCallback());
//        template.setReturnsCallback(returnsCallback());
//        return template;
//    }
//
//    @Bean
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//    public RabbitTemplate.ConfirmCallback confirmCallback() {
//        RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
            /**
             * 消息到交换机的确认
             *
             * @param correlationData 配置信息
             * @param ack             交换机确认  true消息接受成功  false消息接受失败
             * @param cause           消息发送失败原因
             */
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("-----------进入了correlationData方法---------------");
//                System.out.println("回调id：" + correlationData);
//                System.out.println("交换机确认:" + ack);
//                System.out.println("消息发送失败原因:" + cause);
//                if (ack) {
//                    System.out.println("消息发送成功");
//                } else {
//                    System.out.println("消息发送失败,存入数据库，启动定时重发");
//                }
//            }
//        };
//        return confirmCallback;
//    }
//    @Bean
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//    public RabbitTemplate.ReturnsCallback returnsCallback(){
//        RabbitTemplate.ReturnsCallback returnsCallback = new RabbitTemplate.ReturnsCallback(){
//            @Override
//            public void returnedMessage(ReturnedMessage returned) {
//                System.out.println("----------进入了returnsCallback方法---------");
//                System.out.println("消息没有进入队列");
//                int code = returned.getReplyCode();
//                System.out.println("相应码==>"+code);
//                String replyText = returned.getReplyText();
//                System.out.println("响应原因==>"+replyText);
//                System.out.println("returned==>"+returned);
//            }
//        };
//        return returnsCallback;
//    }


